Using the blue_jays.rda dataset, recreate the following graphic as precisely as possible.
Hints:
Transparency is 0.8
Point size 2
Create a label_info dataset that is a subset of original data, just with the 2 birds to be labeled
Shift label text horizontally by 0.5
See ggplot2 textbook 8.3 building custom annotations
Annotation size is 4
Classic theme
Solution
Code
#data set for top head size for each sexlabel_info <- blue_jays %>%#largest to smallestarrange(desc(Head)) %>%# group by sexgroup_by(KnownSex) %>%#take the top 2 head sizes for each group, to account for female pointtop_n(n =2, wt = Head)
Code
#caption caption <-paste("Head length versus body mass for", nrow(blue_jays), "blue jays")# M and F labelsLabels <- label_info[c(1,4),]#make plot ggplot(data = blue_jays, aes(x = Mass, y = Head, color = KnownSex)) +geom_point(alpha =0.8, size =2) +annotate("text", label = caption, hjust =0, vjust =1, size =4) +labs(x =" Body mass (g)", y ="Head length (mm)") +geom_text(data = Labels, aes(label = KnownSex), nudge_x =0.5) +guides(color ="none") +theme_classic()
Exercise 2
Using the tech_stocks dataset, recreate the following graphics as precisely as possible. Use the column price_indexed.
Plot 1
Hints:
Create a label_info dataset that is a subset of original data, just containing the last day’s information for each of the 4 stocks
serif font
Annotation size is 4
Solution
Code
# df with most recent stock infolabel_info <- tech_stocks %>%ungroup() %>%#arrange by datearrange(desc(date)) %>%distinct(company, .keep_all =TRUE)
Code
#caption caption <-paste("Stock price over time for four major tech companies")#range for x and y variables xrange <-range(tech_stocks$date)yrange <-range(tech_stocks$price_indexed)tech_stocks <- tech_stocks %>%ungroup()#lineplotggplot(data = tech_stocks, aes(x = date, y = price_indexed)) +geom_line(aes(color = company)) +# remove all legends #theme(legend.position = "none") +#remove x label xlab(NULL) +ylab("Stock price, indexed") +annotate("text", x = xrange[1], y = yrange[2] , label = caption, hjust =0, vjust =1, family ="serif", size =4) +#company labelsgeom_text(data = label_info, aes(label = company)) +guides(color ="none") +theme_minimal()
Use the athletes_dat dataset — extracted from Aus_althetes.rda — to recreate the following graphic as precisely as possible. Create the graphic twice: once using patchwork and once using cowplot.
Code
# Get list of sports played by BOTH sexesboth_sports <- Aus_athletes %>%# dataset of columns sex and sport # only unique observationsdistinct(sex, sport) %>%# see if sport is played by one gender or bothcount(sport) %>%# only want sports played by BOTH sexesfilter(n ==2) %>%# get list of sportspull(sport)# Process dataathletes_dat <- Aus_athletes %>%# only keep sports played by BOTH sexesfilter(sport %in% both_sports) %>%# rename track (400m) and track (sprint) to be track# case_when will be very useful with shiny appsmutate(sport =case_when( sport =="track (400m)"~"track", sport =="track (sprint)"~"track",TRUE~ sport ) )
Hints:
Build each plot separately
Bar plot: lower limit 0, upper limit 95
Bar plot: shift bar labels by 5 units and top justify
Bar plot: label size is 5
Bar plot: #D55E00D0 & #0072B2D0 — no alpha
Scatterplot: #D55E00D0 & #0072B2D0 — no alpha
Scatterplot: filled circle with “white” outline; size is 3
Scatterplot: rcc is red blood cell count; wcc is white blood cell count
Boxplot: outline #D55E00 and #0072B2; shading #D55E0040 and #0072B240
Boxplot: should be made narrower; 0.5
Boxplot: Legend is in top-right corner of bottom plot
Boxplot: Space out labels c("female ", "male")
Boxplot: Legend shading matches hex values for top two plots